﻿/*
 * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：研发部
 * 日 期：2017.03.22
 * 描 述：数据字典管理	
 */
var refreshGirdData; // 更新数据
var selectedRow;
var ProjectId = request('ProjectId');

var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                var parentId = $('#gridtable').jfGridValue('DataItemID');
                selectedRow = null;
                learun.layerForm({
                    id: 'ClassifyForm',
                    title: '添加分类',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ClassifyForm?parentId=' + parentId + "&ProjectId=" + ProjectId,
                    width: 500,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('DataItemID');
                if (learun.checkrow(keyValue)) {
                    var isAllowEdit = $('#gridtable').jfGridValue('IsAllowEdit');
                    if (ProjectId && isAllowEdit != 1) {
                        learun.alert.warning('当前字典分类不允许编辑！');
                        return false;
                    }
                    learun.layerForm({
                        id: 'ClassifyForm',
                        title: '编辑分类',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/ClassifyForm?ProjectId=' + ProjectId,
                        width: 500,
                        height: 400,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('DataItemID');
                if (learun.checkrow(keyValue)) {
                    var isAllowEdit = $('#gridtable').jfGridValue('IsAllowEdit');
                    if (ProjectId && isAllowEdit != 1) {
                        learun.alert.warning('当前字典分类不允许删除！');
                        return false;
                    }
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(top.$.rootUrl + '/ZZDT_EC/ec_dataitem/DeleteForm', { keyValue: keyValue, ProjectId: ProjectId }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/GetList',
                headData: [
                    { label: '名称', name: 'DataItemName', width: 200, align: 'left' },
                    { label: '编号', name: 'DataItemCode', width: 200, align: 'left' },
                    { label: '排序', name: 'OrderID', width: 50, align: 'center' },
                    {
                        label: "树型", name: "IsTree",width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                        label: "有效", name: "IsEnabled",width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                        label: "是否允许编辑", name: "IsAllowEdit", width: 90, align: "center", ishide: ProjectId ? true : false,
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    { label: "备注", name: "Remark", width: 200, align: "left" },
                    { name: "",width: 1 }
                ],
                isTree: true,
                mainId: 'DataItemID',
                parentId: 'UpDataItemID',
                reloadSelected:true
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.keyword = $('#txt_Keyword').val();
            param.ProjectId = ProjectId;
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        //$('#gridtable').jfGridSet('reload');
        location.reload();
    }

    page.init();
}


